package com.budwk.app.sqfwV2.generator.shawenTown.excel;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONObject;
import com.budwk.app.sqfw.constants.CommonConstants;
import com.budwk.app.sqfw.constants.DictConstants;
import com.budwk.app.sqfw.models.Sqfw_formSummary_new;
import com.budwk.app.sqfwDoc.bo.SqfwExcelForSummaryBO;
import com.budwk.app.sqfwDoc.bo.SqfwExcelJudgeRes;
import com.budwk.app.sqfwDoc.codeGenerator.excelGenerate.BaseExcelParamDTO;
import com.budwk.app.sqfwDoc.codeGenerator.excelGenerate.BaseTemplateExcelGenerator;
import com.budwk.app.sqfwDoc.codeGenerator.excelGenerate.SqfwExcelCreateCommonBO;
import com.budwk.app.sqfwV2.component.DayingRoadPdfComponent;
import com.budwk.app.sqfwV2.component.ShawenTownComponent;
import com.budwk.app.sqfwV2.dto.image.ExcelProblemImageConfigBO;
import com.budwk.app.sqfwV2.dto.shawenTown.SWTownProblemVo;
import com.budwk.app.sqfwV2.dto.shawenTown.ShawenTownSafetyPdfDTO;
import com.budwk.app.sqfwV2.enums.FileServiceConfigEnum;
import com.budwk.app.sys.services.SysDictService;
import com.budwk.app.sys.vo.SysDictVO;
import com.budwk.app.util.StrUtil;
import com.budwk.app.util.poiUtils.CommonFormOfficeUtils;
import com.budwk.app.util.poiUtils.excelPictureConfigBO.ExcelFileCreateParamBO;
import com.budwk.app.util.poiUtils.nutzBeanUtils.NutzIocUtils;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.*;

/**
 * @author 东方祥云低代码1.1
 * @since 2024-03-11
 *
 * 沙文消防安全排查 ShawenTownSafety
 * 消防宣传台账.xlsx
 */

@Slf4j
public class FirePublicityLedgerGenerator extends BaseTemplateExcelGenerator {

    @Override
    protected String getTemplateName() {
        return FileServiceConfigEnum.BYC.getFilePath();
    }

    @Override
    public String getDownloadFileName() {
        return FileServiceConfigEnum.BYC.getFileName();
    }

    /**
     * 表头信息
     * @param excelParam
     * @param excelCreateParam
     */
    @Override
    protected void customerPopularSheetBaseParam(Map<String, Object> excelParam, SqfwExcelCreateCommonBO excelCreateParam) {
        BaseExcelParamDTO baseExcelParamDTO = excelCreateParam.getBaseExcelParamDTO();
        String county = CommonConstants.DIRECTLY_UNDER.equals(baseExcelParamDTO.getCounty()) ? "" : baseExcelParamDTO.getCounty();
        String community = CommonConstants.DIRECTLY_UNDER.equals(baseExcelParamDTO.getCommunity()) ? "" : baseExcelParamDTO.getCommunity();
        excelParam.put("comunity", String.format("%s%s", StrUtil.formatStr(county), StrUtil.formatStr(community)));
    }

    /**
     * 内容数据
     * @param listMap
     * @param excelCreateParam
     */
    @Override
    protected void customerPopularSheetDataParam(List<Map<String, Object>> listMap, SqfwExcelCreateCommonBO excelCreateParam) {
        List<Sqfw_formSummary_new> summaryList = DayingRoadPdfComponent.getSummaryList(listMap, excelCreateParam);
        int num = 1;
        SysDictService dictService = NutzIocUtils.getBean(SysDictService.class);
        List<SysDictVO> mainBussinessCodes = dictService.listDictByCode(DictConstants.SHAWEN_BUSSINESS_SCOPE);
        List<Sqfw_formSummary_new> newList = generateNewData(summaryList);
        for (Sqfw_formSummary_new summary : newList) {
            ShawenTownSafetyPdfDTO roadPdfDTO = JSONObject.parseObject(summary.getParamData(), ShawenTownSafetyPdfDTO.class);
            // 构建数据
            Map<String, Object> map = buildExcelMap(roadPdfDTO, summary, mainBussinessCodes);
            map.put("num", num ++);
            listMap.add(map);
        }
    }

    private Map<String, Object> buildExcelMap(ShawenTownSafetyPdfDTO roadPdfDTO, Sqfw_formSummary_new summary, List<SysDictVO> mainBussinessCodes) {
        Map<String, Object> map = new HashMap<>();
        map.put("address", StrUtil.replaceNullStr(roadPdfDTO.getAddress()));
        map.put("operator", StrUtil.replaceNullStr(roadPdfDTO.getOperator()));
        map.put("createUserName", StrUtil.replaceNullStr(summary.getCreateUserName())); // 排查人员
        ShawenTownComponent.formatFillDate(roadPdfDTO, map); // 排查时间
        map.put("patrolContents", StrUtil.replaceNullStr(roadPdfDTO.getPatrolContents())); // 排查情况
        // 巡查图片
        map.put("visitPictures", Lists.newArrayList());
        if (StringUtils.isNotEmpty(roadPdfDTO.getVisitPictures())) {
            map.put("visitPictures", CommonFormOfficeUtils.addPictureToByteList(roadPdfDTO.getVisitPictures(), 5));
        }
        // 签字及电话
        ShawenTownComponent.excelSignature(roadPdfDTO.getCheckedUnitLeader(), summary.getCreateUserName(), map);
        map.put("operatorTelephone", StrUtil.replaceNullStr(roadPdfDTO.getOperatorTelephone()));
        map.put("remark", StringUtils.EMPTY);
        // 经营范围
        String mainBusinessScope = roadPdfDTO.getSmallBusinessEstablishment().getMainBusinessScope();
        map.put("mainBusinessScope", CommonConstants.HORIAONTAL_LINE);
        if (StringUtils.isNotEmpty(mainBusinessScope)) {
            Optional<SysDictVO> optional = mainBussinessCodes.stream().filter(e -> e.getCode().equals(mainBusinessScope)).findFirst();
            optional.ifPresent(sysDictVO -> map.put("mainBusinessScope", sysDictVO.getName()));
        }
        return map;
    }

    /**
     * 是否为小型经营场所等于是 生成表格
     * 1.1：只要“是否宣传消防知识和消防应知应会”为“是”，就进入“消防宣传台账”
     * @return
     */
    @Override
    public List<Sqfw_formSummary_new> generateNewData(List<Sqfw_formSummary_new> summaryList) {
        if (CollectionUtil.isEmpty(summaryList)) {
            return summaryList;
        }
        List<Sqfw_formSummary_new> newList = new ArrayList<>();
        for (Sqfw_formSummary_new summary : summaryList) {
            ShawenTownSafetyPdfDTO roadPdfDTO = JSONObject.parseObject(summary.getParamData(), ShawenTownSafetyPdfDTO.class);
            SWTownProblemVo isKnowledge = roadPdfDTO.getSmallBusinessEstablishment().getIsKnowledge();
            if (isKnowledge == null) {
                newList.add(summary);
                continue;
            }
            if (CommonConstants.TRUE.equals(isKnowledge.getKey())) {
                newList.add(summary);
            }
        }
        return newList;
    }

    @Override
    protected void completeExcelFileCreateParam(ExcelFileCreateParamBO excelFileCreateParam) {

    }

    @Override
    protected void customerPopularRowDataMap(Map<String, Object> rowDataMap, SqfwExcelForSummaryBO forSummaryBO) {

    }

    @Override
    protected SqfwExcelJudgeRes judgeDataIntoExcelBeforeErgodic(SqfwExcelCreateCommonBO excelCreateParam, Map<String, Object> excelParam) {
        return null;
    }

    @Override
    protected SqfwExcelJudgeRes judgeDataIntoExcelBeforeCreate(Map<String, Object> excelParam) {
        return null;
    }

    @Override
    protected Class<?> getImageConfigBO() {
        return ExcelProblemImageConfigBO.class;
    }
}
